// 获取栏目下的栏目
function getTypeListByTypeId(obj) {
  if (!obj) {
    return new Error('缺少参数！');
  }
  var TYPEID = obj.TYPEID;
  var LENGTH = obj.LENGTH ? obj.LENGTH : 99;
  var STATION = obj.STATION ? obj.STATION : 0;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/getTypeListByTypeId?TYPEID=' + TYPEID + '&LENGTH=' + LENGTH + '&STATION=' + STATION,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getTypeListByTypeId接口错误!');
    }
  })
  return _data;
}


// 获取栏目下的节目
function getVodListByTypeId(obj) {
  if (!obj) {
    return new Error('缺少参数！');
  }
  var TYPEID = obj.TYPEID;
  var LENGTH = obj.LENGTH ? obj.LENGTH : 99;
  var STATION = obj.STATION ? obj.STATION : 0;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/getVodListByTypeId?TYPEID=' + TYPEID + '&LENGTH=' + LENGTH + '&STATION=' + STATION,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getVodListByTypeId接口错误!');
    }
  })
  return _data;
}



// 获取节目详情
function getVodDetailInfo(obj) {
  if (!obj.VODID) {
    return new Error('缺少参数！');
  }

  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/getVodDetailInfo?account=' + USERID + '&VODID=' + obj.VODID,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getVodListByTypeId接口错误!');
    }
  })
  return _data;
}

// 获取节目在栏目列表中第几页
function getVodPageIndex(obj) {
  if (!obj) {
    return new Error('缺少参数！');
  }
  var TYPEID = obj.TYPEID;
  var VODID = obj.VODID ? obj.VODID : '';
  var LENGTH = obj.LENGTH ? obj.LENGTH : 99;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/getVodPageIndex?TYPEID=' + TYPEID + '&LENGTH=' + LENGTH + '&VODID=' + VODID,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getVodPageIndex接口错误!');
    }
  })
  return _data;
}

// 探针获取
function getLog(obj) {
  if (!obj) {
    return new Error('缺少参数！');
  }
  var programId = obj.programId ? obj.programId : '';
  var programName = obj.programName ? obj.programName : '';
  var ctype = obj.ctype ? obj.ctype : '';
  var account = obj.account ? obj.account : '';
  var usermac = obj.usermac ? obj.usermac : '';
  var startTime = obj.startTime ? obj.startTime : '';
  var endTime = obj.endTime ? obj.endTime : '';
  var platform = obj.platform ? obj.platform : '';
  var _data = null;
  var param = ''
  if (startTime && endTime) {
    param = '&startTime=' + startTime + '&endTime=' + endTime
  }
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/visit?programId=' + programId + '&programName=' +
      programName + '&ctype=' + ctype + '&account=' + account + '&usermac=' +
      usermac + '&platform=' + platform + param,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getLog接口错误!');
    }
  })
  return _data;
}


// 添加地址
function addReturnPage(page) {
  if (!page) return
  $.ajax({
    type: 'get',
    url: IP + 'restApi/addReturnPage?platform=1&account=' + USERID + '&page=' + page,
    success: function (data) {
      // _data = data;
    },
    error: function () {
      return new Error('addReturnPage接口错误!');
    }
  })
}

// 获取地址
function getReturnPage() {
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/getReturnPage?platform=1&account=' + USERID,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getReturnPage接口错误!');
    }
  })
  return _data
}

// 清空地址
function clearReturnPage() {
  $.ajax({
    type: 'get',
    url: IP + 'restApi/clearReturnPage?platform=1&account=' + USERID,
    success: function (data) {},
    error: function () {
      return new Error('clearReturnPage接口错误!');
    }
  })
}

// 添加主页地址
function addDomainPath(page) {
  if (!page) return
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/addDomainPath?platform=1&account=' + USERID + '&page=' + page,
    success: function (data) {
      // _data = data;
    },
    error: function () {
      return new Error('addDomainPath接口错误!');
    }
  })
}

// 获取主页地址
function getDomainPath() {
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/getDomainPath?platform=1&account=' + USERID,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getDomainPath接口错误!');
    }
  })
  return _data;
}

// 清空主页信息
function clearDomainPath(){
  $.ajax({
    type: 'get',
    url: IP + 'restApi/clearDomainPath?platform=1&account=' + USERID,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getDomainPath接口错误!');
    }
  })
}


// 搜索
function search(obj) {
  var _data = null;
  var QUERY = obj.QUERY;
  var LENGTH = obj.LENGTH ? obj.LENGTH : 8;
  var STATION = obj.STATION ? obj.STATION : 1;
  if (!QUERY) {
    return new Error('缺少参数！');
  }
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/getVodListBySearchName?platform=1&searchName=' + QUERY + '&LENGTH=' + LENGTH + '&STATION=' + STATION,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('search接口错误!');
    }
  })
  return _data;
}


// 添加播放记录
function addPlayHistory(programId, programName) {
  if (!programId || !programName) return
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/addPlayHistory?ctype=剧头&position=0&platform=1&account=' + USERID + '&usermac=' + USERID + '&programId=' + programId + '&programName=' + programName,
    success: function (data) {
      // console.log(data)
    },
    error: function () {
      return new Error('addPlayHistory接口错误!');
    }
  })
  var total = getHistoryList({
    length: 1
  }).total;
  var data = getHistoryList({
    length: total
  });
  while(total > 12){
    delPlayHistory(data.data[--total].programId);
  }
}

// 获取播放列表
function getHistoryList(obj) {
  var len = obj.length ? obj.length : 12
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/getHistoryList?platform=1&LENGTH=' + len + '&STATION=1&account=' + USERID,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getHistoryList接口错误!');
    }
  })
  return _data;
}

// 删除播放纪录
function delPlayHistory(programId) {
  if (!programId) return
  $.ajax({
    type: 'get',
    url: IP + 'restApi/delPlayHistory?account=' + USERID + '&programId=' + programId,
    success: function (data) {

    },
    error: function () {
      return new Error('delPlayHistory接口错误!');
    }
  })
}

// 添加收藏
function addFavorite(programId, programName) {
  if (!programId || !programName) return
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/addFavorite?platform=1&ctype=1&account=' + USERID + '&usermac=' + USERID + '&programId=' + programId + '&programName=' + programName,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('addFavorite接口错误!');
    }
  })
  return _data
}


// 取消收藏
function delFavorite(programId) {
  if (!programId) return
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/delFavorite?programId=' + programId + '&account=' + USERID,
    success: function (data) {
      _data = data;

    },
    error: function () {
      return new Error('delFavorite接口错误!');
    }
  })
  return _data
}


// 收藏列表
function getFavList(page) {
  page = page ? page : 1;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: IP + 'restApi/getFavList?account=' + USERID + '&platform=1&LENGTH=12&STATION=' + page,
    success: function (data) {
      _data = data;

    },
    error: function () {
      return new Error('delFavorite接口错误!');
    }
  })
  return _data
}